<?php
// @formatter:off
/**
 * @file WCMassiveInstaller.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: WCMassiveInstaller.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/db/WCMassiveInstaller.php $
 */
// @formatter:on

if(!$wcDefaults["installed"]) {
	require_once "{$wcPaths["includes-directory"]}/db/WCDBTable.php";
	require_once "{$wcPaths["includes-directory"]}/db/WCDBIndex.php";
}

/**
 * @class WCMassiveInstaller
 */
class WCMassiveInstaller {
	/**
	 * @var WCMassiveInstaller
	 */
	protected static $_Instance;

	protected $_db;
	protected $_dbprefix;
	protected $_structures = array();

	protected function __construct() {
		global $wcDatabaseAccess;
		global $wcDefaults;

		$this->_db = WCConnection::Instance();
		$this->_dbprefix = $wcDatabaseAccess["prefix"];
	}
	public function __destruct() {
	}
	/**
	 * Prevent users to clone the instance.
	 */
	public function __clone() {
		trigger_error(__CLASS__.": Clone is not allowed.", E_USER_ERROR);
	}
	//
	// Public methods
	public function install() {
		global $wcDefaults;
		//
		// Loading global pointers.
		global $wcDefaults;
		//
		// Checking that Read Only Mode is not activated.
		if(!$wcDefaults["read-only"]) {
			global $wcDatabase;

			//
			// A service must avoid any database alteration information.
			$silentDatabase = defined("__SERVICE__") || !$wcDefaults["debug"];

			//
			// Prompts on web pages require special tags like '<!--' and '-->'
			$onWeb = !defined("__SHELL__");

			if(!$wcDefaults["installed"]) {
				$tables = array();
				$queries = array();

				foreach($wcDatabase["tables"] as $table) {
					$item = new WCDBTable($table);
					$tables[] = $item->name();
					if(!$item->check()) {
						foreach($item->ddlQueries() as $query) {
							$queries[] = $query;
						}
					}
				}

				foreach($wcDatabase["indexes"] as $index) {
					$item = new WCDBIndex($index);
					if(!$item->check()) {
						foreach($item->ddlQueries() as $query) {
							$queries[] = $query;
						}
					}
				}

				if($wcDatabase["aggressive"]) {
					foreach($this->removeUnknownTables($tables) as $query) {
						$queries[] = $query;
					}
				}

				if(!$silentDatabase && $onWeb && isset($queries[0])) {
					echo "<!--\n";
				}

				foreach($queries as $query) {
					if($query) {
						if(!$silentDatabase) {
							echo "Query: {$query}\n";
						}
						$this->_db->exec($query);
					}
				}

				if(!$silentDatabase && $onWeb && isset($queries[0])) {
					echo "-->\n";
				}
			}
		}
	}
	//
	// Protected methods
	protected function removeUnknownTables(&$valids) {
		$queries = array();

		if(count($valids) > 0) {
			global $wcDatabaseAccess;

			$query = "select  table_name\n";
			$query .= "from    information_schema.tables\n";
			$query .= "where   table_name not in ('".implode("','", $valids)."')\n";
			$query .= " and    table_schema = '{$wcDatabaseAccess["database"]}'\n";
			$result = $this->_db->query($query, true);
			if($result && $result->rowCount() > 0) {
				foreach($result->fetchAll() as $row) {
					$queries[] = "drop table {$row["table_name"]} cascade";
				}
			}
		}

		return $queries;
	}
	//
	// Public class methods
	public static function Instance() {
		if(!isset(self::$_Instance)) {
			$c = __CLASS__;
			self::$_Instance = new $c();
		}

		return self::$_Instance;
	}
}
?>